<?
	/* Youtube - stream reader plugin */	

	// return type of service
	function youtube_getType() { return "media"; }


	// extra the service- action  line ("ike: "I tweeted on twitter.com")
	function youtube_getAction($line) {
		$entry=explode("|",$line);
		
		$entry[7]=trim($entry[7]);
		if ($entry[7]=="uploads") return "<a href='?service=youtube'><img class='icon' src='".serviceIcon("youtube")."'/></a> I uploaded a movie to <a href='http://youtube.com'>youtube.com</a>";
		if ($entry[7]=="favorites") return "<a href='?service=youtube'><img class='icon' src='".serviceIcon("youtube")."'/></a> I liked a movie on <a href='http://youtube.com'>youtube.com</a>";
	}

	// extract and return the correct caption
	function youtube_getCaption($line) {
		$entry=explode("|",$line);
		return null;	// $entry[6];  // caption is given in youtube movie itself
	}

	// extract and return the correct post
	function youtube_getPost($line) {
		$entry=explode("|",$line);
		$itemPost='<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/'.$entry[3].'&fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'.$entry[3].'&hl=nl&fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object><br/>';
		return $itemPost;
	}

	// extract and return the correct link to the item
	function youtube_getLink($line) {
		$entry=explode("|",$line);
		return 'http://www.youtube.com/watch?v='.$entry[3];
	}


	// display information about us with a link to the profile
	//  -- this is called to display list of services that are linked (admin-functions.php)
	function youtube_identify($item) {
		$entry=explode("|",$item);
		
		if (strpos($entry[3],"uploads")) return "<a href='".$entry[3]."'>".$entry[2]." uploads</a>";
		if (strpos($entry[3],"favorites")) return "<a href='".$entry[3]."'>".$entry[2]." favorites</a>";
	}

	
	function youtube_form() {
		print "Username: <input type='text' name='username'> <input type='submit' name='add' value='add'><br/>";
		print "favorites: <input type='checkbox' name='favorites' checked> ";
		print "uploads: <input type='checkbox' name='uploads' checked>";
	}
	



	// get service + profile information
	function youtube_addProfile() {
			$serviceID=$_POST['addservice'];
			$username=$_POST['username'];
	
			// include uploads
			if (isset($_POST['uploads'])) {
				$rsslink="http://gdata.youtube.com/feeds/base/users/".$username."/uploads";
				writeService($serviceID,$username,$rsslink);
			}
			
			// include favorites
			if (isset($_POST['favorites'])) {
				$rsslink="http://gdata.youtube.com/feeds/base/users/".$username."/favorites?client=ytapi-youtube-user&v=2";
				writeService($serviceID,$username,$rsslink);
			}
	}

	
	function youtube_download($serviceid,$filename)  {
		$reader = new XMLReader();
		$reader->open($filename);

		set_time_limit(30);
		$count=0;
		
		$itemids=array();
		$continueParsing=true;

		while ($reader->read() && $continueParsing) {
				if ($reader->nodeType==XMLREADER::ELEMENT) {
				
					if ($reader->localName=="entry") {
						$guid=null;
						$title=null;
						$mediaurl=null;
						$thumb=null;
						$link=null;
						$pubdate=null;
						$upddate=null;
						
						while ($reader->read()) {

							if ($reader->localName=="id" && !$guid) {
								$reader->read();
								$guid=$reader->value;
							}

							/*
							if ($reader->localName=="link" && !$link) {
								$reader->read();
								$link=$reader->value;
							}
							*/
							
							
							if ($reader->localName=="updated" && !$upddate) {
								$reader->read();
								$upddate=$reader->value;
							}
							
							if ($reader->localName=="published" && !$pubdate) {
								$reader->read();
								$pubdate=$reader->value;
							}

							if ($reader->name=="title" && !$title) {
								$reader->read();
								$title=$reader->value;
							}
		

							if ($reader->name=="link" && !$mediaurl) {
								$mediaurl=$reader->getAttribute("href");
								// we only need the video code!
								$mediaurl=substr($mediaurl,strpos($mediaurl,"?v=")+3);
								$guid=$mediaurl;
							}
		
							
							if ($reader->localName=="entry") {
								if ($guid) {
								
									$indx=date("YmdHis",strtotime($pubdate));
									/*
									if (strpos($filename,"uploads")) {
									} else {
										$indx=date("YmdHis",strtotime($upddate));
									}
									*/
									
									//if (newGuid($guid)) {
										$item[$indx]->title=$title;
										$item[$indx]->post=$mediaurl;
										$item[$indx]->link=$link;
										$item[$indx]->guid=$guid;
										$item[$indx]->extra="|".$link."|".$title."|";
										$item[$indx]->pubDate=$pubdate;

										if (strpos($filename,"uploads")) {
											$item[$indx]->extra.="uploads|";
											//$item[$indx]->pubDate=$pubdate;
										}
										
										if (strpos($filename,"favorites")) {
											$item[$indx]->extra.="favorites|";
											//$item[$indx]->pubDate=$upddate;
										}

									//} else $continueParsing=false;
									
								}
								break;
							}
						}
						
						//print $item[$guid]["title"]." : <img src='".$item[$guid]["media"]."'>";
					}
					set_time_limit(30);
				}
				
		}  
		
		insertEntries($item,"youtube",$serviceid);
	}
?>